package com.wxs.pojo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/*代理角色*/
public class ProxyInvocationHandler implements InvocationHandler {
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }

    //生成代理类，重点是第二个参数，获取要代理的抽象角色！之前都是一个角色，现在可以代理一类角色
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                target.getClass().getInterfaces(),this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        seeHose();
        Object res = method.invoke(target, args);
        fare();
        return res;
    }

    public void seeHose(){
        System.out.println("带房客看房");
    }

    public void fare(){
        System.out.println("收中介费");
    }

    public void log(String methodName){
        System.out.println("执行了"+methodName+"方法");
    }
}
